<?php
namespace Home\Controller;

use Think\Controller;

class OrdersController extends CustomerController {

	public function _initialize() {
		parent::_initialize();
		if ($this->cart_array['cart_items'] <= 0) {
			$this->redirect("Product/shopcart");
		}
	}

	public function checkout() {
		$oop = M("address");
		$con['addr_cs_id'] = $this->user_info['cs_id'];
		$con['addr_status'] = 1;
		$con['addr_type'] = 1; //billing
		$billing = $oop->where($con)->select();

		$con['addr_type'] = 0; //shipping
		$shipping = $oop->where($con)->select();

		$this->assign("billing", $billing);
		$this->assign("shipping", $shipping);

		//支付手段

		$payment_list = M("payment")->order("pm_sort desc")->select();
		foreach ($payment_list as $key => $value) {
			$payment_list[$key]['other'] = Pay("Common/" . $value['pm_name'] . "/params");
		}
		//dump($payment_list);
		$this->assign("payment_list", $payment_list);
		$this->display();
	}

	public function confirm() {

		if (cookie("order_id")) {
			$this->error("Error re-submitting,you're redirected to shopping cart...", U("Product/shopcart"));
		}

		if (!I("bill_id") || !I("ship_id") || !I("payment_name")) {
			$this->redirect("Orders/checkout");
		}

		$billing = M("address")->find(I("bill_id"));
		$shipping = M("address")->find(I("ship_id"));
		$pay_con['pm_name'] = I("payment_name");
		$payment = M("payment")->where($pay_con)->find();

		$oop = M("orders");
		$data["order_amount"] = $this->cart_array['cart_amount'];
		//$data["order_status"]=;
		//$data["order_time"]=;
		$data["order_ip"] = get_client_ip();
		$data["order_cs_id"] = $this->user_info['cs_id'];
		$data["order_paymethod"] = I("payment_name");
		//$data["order_isdelivered"]=;
		$data["bill_addr_id"] = $billing['addr_id'];
		$data["bill_addr_cs_id"] = $billing['addr_cs_id'];
		$data["bill_addr_country"] = $billing['addr_country'];
		$data["bill_addr_state"] = $billing['addr_state'];
		$data["bill_addr_city"] = $billing['addr_city'];
		$data["bill_addr_street"] = $billing['addr_street'];
		$data["bill_addr_zip"] = $billing['addr_zip'];
		$data["bill_addr_phone"] = $billing['addr_phone'];
		$data["bill_addr_firstname"] = $billing['addr_firstname'];
		$data["bill_addr_lastname"] = $billing['addr_lastname'];
		$data["bill_addr_gender"] = $billing['addr_gender'];

		$data["ship_addr_id"] = $shipping['addr_id'];
		$data["ship_addr_cs_id"] = $shipping['addr_cs_id'];
		$data["ship_addr_country"] = $shipping['addr_country'];
		$data["ship_addr_state"] = $shipping['addr_state'];
		$data["ship_addr_city"] = $shipping['addr_city'];
		$data["ship_addr_street"] = $shipping['addr_street'];
		$data["ship_addr_zip"] = $shipping['addr_zip'];
		$data["ship_addr_phone"] = $shipping['addr_phone'];
		$data["ship_addr_firstname"] = $shipping['addr_firstname'];
		$data["ship_addr_lastname"] = $shipping['addr_lastname'];
		$data["ship_addr_gender"] = $shipping['addr_gender'];

		$data['order_currency'] = $this->web_config['web_currency'];

		//dump($data);exit;
		//dump($this->cart_array['cart_arr']['items']);exit;

		if ($oid = $oop->add($data)) {
			cookie("order_id", $oid);
			//订单历史
			$this->create_orderhistory($oid, 1, "New order created...");
			$this->create_orderdetails($oid, $this->cart_array['cart_arr']['items']);
			$payment_data = json_decode($payment['pm_data'], true);
			$result = Pay("Common/" . $payment['pm_name'] . "/process", array($oid, $payment_data, $data));
			$result['oid'] = $oid;
			switch ($result['status']) {
			case 2: //approved
				$this->create_orderhistory($oid, 2, $result['info']);
				$this->cart->clear();
				break;
			case 3: //declined
				$this->create_orderhistory($oid, 3, $result['info']);
				break;
			default:
				$result['status'] = 1;
			}
			$this->assign($result);
			$this->assign($data);
			$this->display();

		} else {
			$this->error("Error when creating order");
		}

	}

	private function create_orderhistory($oid, $status, $remark = "") {
		$oop = M("orderhistory");
		$data['oh_order_id'] = $oid;
		$data['oh_status'] = $status;
		$data['oh_remark'] = $remark;
		$oop->add($data);
	}

	private function create_orderdetails($oid, $arr) {
		foreach ($arr as $key => $value) {
			$data[$key]['odd_order_id'] = $oid;
			$data[$key]['odd_quantity'] = $value['data']['quantity'];
			$data[$key]['product_id'] = $value['data']['pid'];
			$data[$key]['product_name'] = $value['data']['product_name'];
			$data[$key]['product_price'] = $value['data']['price'];
			$data[$key]['product_dir'] = $value['data']['dir'];
			$data[$key]['product_img'] = $value['data']['img'];
			$data[$key]['product_desc'] = $value['data']['product_desc'];
			$data[$key]['product_cat'] = $value['data']['product_cat'];
		}
		M("orderdetails")->addAll($data);
	}

}